<template>
  <div class="publish-container">
    <el-card>
      <div slot="header">
        <my-breadcrumb>编辑文章</my-breadcrumb>
      </div>
      <el-form label-width="80px" :rules="rules" :model="form"  v-loading="isLoading">
        <el-form-item prop="title" label="标题">
          <el-input v-model="form.title" placeholder="请输入标题，长度在5到30之间" />
        </el-form-item>

        <el-form-item prop="content"  label="内容">
          <el-input placeholder="请输入内容，最少20个字"/>
          <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
        </el-form-item>

        <el-form-item  prop="channelId" label="封面">
          <el-radio-group v-model="form.coverType" @change="hCoverTypeChange">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item  v-if="form.coverType>0">
          <!-- 使用封面组件 -->
        <el-row>
          <el-col :span="6" v-for="(item,idx) in form.coverType" :key="idx">
             <my-cover v-model="form.coverImages[idx]"></my-cover>
          </el-col>
        </el-row>
        </el-form-item>
       <!--
          v-model中的form.channel_id 就是 mychannel的value

          edit.vue创建时，把 -1 传给了子组件
        后来，发了ajax，取得了数据，channel_id 变成了2
        传给子组件的prop - value也变成了2 -->
        <my-channel v-model="form.channel_id"></my-channel>

        <el-form-item>
          <el-button type="primary" @click="hSave" :loading="isLoading">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'edit',
  components: {
    quillEditor
  },
  data () {
    return {
      // 自定义富文本编辑器的工具栏
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      form: {
        coverImages: [], // 保存对应的封面图的地址
        // 1:单图，3：三图，0：无图，-1:自动
        coverType: 0,
        title: '只在人群中多看了你一眼',
        content: '文章的正文............',
        channel_id: -1 // 频道编号
      },
      isLoading: false, // 菊花转起来
      // 表单验证
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.loadArticle()
  },
  methods: {
    async  loadArticle () {
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/articles/' + this.$route.params.id,
          method: 'get'
        })
        console.log(res)
        // 保存数据
        this.form.title = res.data.data.title
        this.form.content = res.data.data.content
        this.form.id = res.data.data.id
        this.form.coverType = res.data.data.cover.type
        this.form.coverImages = res.data.data.cover.images
        this.form.channel_id = res.data.data.channel_id
      } catch (err) {
        console.log(err)
      }
    },
    // 用户封面切换
    // 注意：封面类型 要与 数组相对应
    hCoverTypeChange () {
      console.log(this.form.coverType)
      // 如果切换到单图模式
      if (this.form.coverType === 1) {
        // 数组里面只能有一张图，可以用length规定长度，只留一张图
        this.form.coverImages.length = 1
      }
    },
    builParams () {
      const { title, content, coverType, coverImages } = this.form
      const data = {
        title: title,
        content: content,
        cover: { // 这里先处理无图的情况
          type: coverType, // 0 就是无图；1 就是一张图
          images: coverImages // 用来装图片的地址
        },
        channel_id: this.form.channel_id
      }
      return data
    },
    async hSave () {
      this.isLoading = true // 加载中
      const data = this.builParams()
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/articles' + this.form.id,
          method: 'put',
          data
        })
        console.log(res)

        this.$message.success('修改成功')
        this.$router.push('/articles')
      } catch (err) {
        console.log(err)
        this.$message.error('修改失败')
      }
      this.isLoading = false // 加载结束
    }
  }
}
</script>

<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
